<!-- QQ授权后检测的中转界面- 已授权已有账号直接跳转 -->
<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container" v-if="!isBind">
    <nav class="tab">
      <a @click="hasAccount = true" :class="{ active: hasAccount }" href="javascript:;">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount = false" :class="{ active: !hasAccount }" href="javascript:;">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :openId="openId" />
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :openId="openId" />
    </div>
  </section>

  <section class="container" v-else>
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import { ref } from 'vue'
import LoginHeader from './login-header'
import LoginFooter from './login-footer'
import CallbackBind from './callback-bind'
import CallbackPatch from './callback-patch'
import QC from 'qc'
import { userQQLogin } from "@/api/user"
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { async } from 'q'

export default {
  name: 'Callback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup() {
    const store = useStore()
    const route = useRoute()
    const router = useRouter()
    const hasAccount = ref(true)
    const openId = ref('')
    // 假设已经绑定，默认会去做一次登录，如果登录失败证明未绑定。
    const isBind = ref(true)
    // 1. 获取QQ互联的openId也就是后台需要的unionId
    // 2. 根据QQ互联的openId去进行登录，准备一个接口
    if (QC.Login.check()) { // 是否授权
      QC.Login.getMe(openID => { // 获取个当前登录用户openID
        openId.value = openID
        userQQLogin(openID).then(async ret => {
          // 代表：使用qq登录成功
          // 1. 存储用户信息
          store.commit('user/setUser', ret.result)
          store.commit('user/setRedirectUrl', route.query.redirectUrl || '/')

          // 合并本地购物车
          await store.dispatch('cart/mergeLocalCart')
          // 2. 跳转到来源页或者首页
          router.push(store.state.user.redirectUrl)
          // 3. 成功提示
          Message({ type: 'success', text: 'QQ登录成功' })
        }).catch(e => {
          // 代表：使用qq登录失败===>1. 没绑定小兔鲜帐号  2. 没有小兔鲜帐号
          isBind.value = false
        })
      })
    }

    return {
      hasAccount, isBind, openId
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../../assets/images/loading.gif) no-repeat center /
        100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>